package com.yfbao.horizon.docker.controller;

import cn.hutool.json.JSONUtil;
import com.yfbao.horizon.docker.entity.TrxTask;
import com.yfbao.horizon.docker.service.TestService;
import com.yfbao.horizon.docker.service.TrxTaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/test")
@Slf4j
public class TestController {
    @Autowired
    private TestService testService;

    @Autowired
    private CacheManager cacheManager;
    @Autowired
    private TrxTaskService trxTaskService;


    @GetMapping("")
    public Integer getRulesNum(){
        testService.testMethod();
        return 1;
    }

    /**
     * 验证2个重名cookie时，增加重名cookie无法覆盖的问题
     * @param request
     * @param response
     * @return
     */

    @GetMapping("/example")
    public String example(HttpServletRequest request, HttpServletResponse response) {
        // 在这里可以使用 request 和 response 对象
        // 例如：获取请求参数、设置响应头等
        Cookie[] cookies = request.getCookies();
        if(cookies!=null){
            for (Cookie cookie : cookies) {
                if(cookie.getName().equals("SY_ACCESS_TOKEN")){
                    cookie.setMaxAge(0);
                    response.addCookie(cookie);
                }
            }
        }
        long l = System.currentTimeMillis();
        Cookie newCookie = new Cookie("SY_ACCESS_TOKEN", "XXXXXXXXX_"+l);
//        newCookie.setDomain("AAAA");
        response.addCookie(newCookie);
        return "examplePage"; // 返回视图名
    }

    @GetMapping("/example2")
    public String example2(HttpServletRequest request, HttpServletResponse response) {
        // 在这里可以使用 request 和 response 对象
        // 例如：获取请求参数、设置响应头等
        Cookie[] cookies = request.getCookies();
        long l = System.currentTimeMillis();
        Cookie newCookie = new Cookie("SY_ACCESS_TOKEN", "XXXXXXXXX22222_"+l);
//        newCookie.setDomain("AAAA");
        response.addCookie(newCookie);
        return "examplePage"; // 返回视图名
    }

    @GetMapping("/creatTrx")
    public String example3(HttpServletRequest request, HttpServletResponse response) {
        // 在这里可以使用 request 和 response 对象
        TrxTask task = trxTaskService.selectById(1L);
        log.info(JSONUtil.parseObj(task).toString());
        return task.getMethod(); // 返回视图名
    }

    @GetMapping("/creatTrx2")
    public String example4(HttpServletRequest request, HttpServletResponse response) {
        // 在这里可以使用 request 和 response 对象
        Cache.ValueWrapper trxTask = cacheManager.getCache("trxTask").get(1L);
        log.info(JSONUtil.parseObj(trxTask).toString());
        return "true"; // 返回视图名
    }


    @PostMapping("/fuzzy/search")
    public Map<String,Object> example5(@RequestBody  Map<String,Object> param) {
        log.info(JSONUtil.toJsonStr(param));
        // 在这里可以使用 request 和 response 对象
//        "success": true,
//                "code": "200",
//                "userMsg": "查询成功",
//                "developerMsg": "",
//                "timestamp": 1717059281318,
//                "data": []
        List<String> list =new ArrayList<>();
        list.add("MB202405160032");
        list.add("MB202405160006");
        list.add("MB202404240025");
        list.add("P202405240002");
        list.add("MB202404240013");

        Map<String,Object> map =new HashMap<>();
        map.put("success",true);
        map.put("code","200");
        map.put("userMsg","查询成功");
        map.put("developerMsg","");
        map.put("timestamp",System.currentTimeMillis());
        map.put("data",list);
        return map; // 返回视图名
    }
}
